﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Collections;

namespace CoolCode.Silverlight.Tests.Web.Models
{
    public class MyValidateAttribute : RequiredAttribute
    {
        //private int Divisor { get; set; }
        //public MyValidateAttribute(int divisor)
        //{
        //    Divisor = divisor;
        //    //ErrorMessage = string.Format("Must be a multiple of {0}.", divisor);
        //}

        //public override bool IsValid(object value)
        //{
        //    if (!(value is int))
        //        return false;
        //    return ((int)value) % Divisor == 0;
        //}

        public override string FormatErrorMessage(string name)
        {
            if (ErrorMessageResourceType.IsSubclassOf(typeof(IDictionary)))
            {
                ResourceWrapper wrapper = new ResourceWrapper();
                string text = wrapper.GetText(ErrorMessageResourceType, ErrorMessageResourceName);

                return string.Format(text, name);
            }
            else
            {
                return base.FormatErrorMessage(name);
            }
        }
    }

    public class ResourceWrapper
    {
        public string GetText(Type type, string name)
        {
            object obj = type.Assembly.CreateInstance(type.FullName);
            IDictionary dict = (IDictionary)obj;
            if (!dict.Contains(name))
                return string.Empty;
            return dict[name].ToString();
        }
    }
}
